import { FetchLoadMeetingRoom, FetchRoomStatic, UserEntity } from '@/api/create/interface'
import { ICriteria, IRoomData, ISelectUser } from './interface'
import dayjs from '@/plugins/dayjs'
import { t } from 'i18next'

export function transRoomData(data: FetchRoomStatic | FetchLoadMeetingRoom): IRoomData {
	return {
		...data,
		data:
			data.data instanceof Array
				? data.data.map((item) => ({
						day: item.day,
						status: item.status,
						meeting_form_key: item.meeting_form_key ?? '',
						meeting_rooms: item.meeting_rooms.map((j) => ({
							id: String(j.id) ?? '',
							name: j.name ?? '',
							e164: j.e164 ?? '',
							region_name: j.region_name,
							region_Id: String(j.region_id) ?? '',
						})),
				  }))
				: [
						{
							day: data.data.day ?? '',
							status: data.data.status ?? 3,
							meeting_form_key: data.data.meeting_form_key ?? '',
							meeting_rooms:
								data.data.meeting_rooms?.map((j) => ({
									id: String(j.id) ?? '',
									name: j.name ?? '',
									e164: j.e164 ?? '',
									region_name: j.region_name,
									region_Id: String(j.region_id) ?? '',
								})) ?? [],
						},
				  ],
	}
}

export function transSelectUserItem(data: UserEntity): ISelectUser {
	return {
		...data,
		uid: `${data.conf_e164 ? data.conf_e164 : ''}$$${data.account_type}$$${data.account}`,
	}
}

export const getDefaultMeetingCriteria: () => ICriteria[] = () => [
	{
		regionName: '',
		deviceIds: '1',
		num: 0,
		regionId: 0,
		upSeating: 0,
		downSeating: 0,
		meetingRoomId: '0',
		meetingRoomsList: [{ label: t('请选择会议室'), value: '0' }],
	},
]

export const getDefaultRoomData = () => ({
	totalNum: 0,
	successNum: 0,
	failureNum: 0,
	status: 0,
	data: [],
})

export const getHourOptions = () => {
	return new Array(25).fill({}).map((_, index) => ({ label: index, value: index }))
}

export const getMinuteOptions = (hour: number) => {
	if (hour === 24) {
		return [{ label: 0, value: 0 }]
	} else {
		return [
			{ label: 0, value: 0 },
			{ label: 15, value: 15 },
			{ label: 30, value: 30 },
			{ label: 45, value: 45 },
		]
	}
}

export const getTimeOptions = () => {
	let start_time_arr = []
	for (let i = 0; i < 24; i++) {
		let m = i < 10 ? '0' + i : '' + i
		for (let j = 0; j < 60; j = j + 15) {
			let s = j < 10 ? '0' + j : '' + j
			let start_time_item = m + ':' + s
			start_time_arr.push({ value: start_time_item, label: start_time_item })
		}
	}
	return start_time_arr
}

//获取会议日期和开始时间
export const getDayAndStartTime = (day: string, startTime: string) => {
	//day-传递的日期，startTime当前时间
	let result = {
		day: day,
		startTime: startTime,
	}
	let hm = startTime.split(':') //hm[0]表示小时，hm[1]表示分钟
	let h = parseInt(hm[0])
	let m = (Math.floor(parseInt(hm[1]) / 15) + 1) * 15
	h = m === 60 ? h + 1 : h //开始的分钟为45之后，小时需要加一
	m = m === 60 ? 0 : m //开始的分钟为45之后，小时需要加一，分钟置为0

	result.day = h === 24 ? dayjs(day, 'YYYY-MM-DD').add(1, 'day').format('YYYY-MM-DD') : day
	h = h === 24 ? 0 : h //每天的开始时间为23:45以后，小时置为0

	let hour = h < 10 ? '0' + h : '' + h
	let minute = m < 10 ? '0' + m : '' + m
	result.startTime = hour + ':' + minute

	return result
}
